<template>
  <div class="app-container flex column">
    <div class="flex align-center" style="position: relative">
      <top-title :policeView="true" :cusString="cusString"></top-title>
      <div class="goback" @click="goback">返回</div>
    </div>
    <div class="content-wrap f1 flex column">
      <!-- 搜索栏 -->
      <div class="search-group">
        <div class="input-wrap">
          <div class="label">机关名称:</div>
          <div class="input-item">
            <a-tree-select
              style="width: 100%"
              :treeData="treeData"
              tree-default-expand-all
              placeholder="请选择机关"
              v-model="queryParam.policeAgencyId"
              @change="handelChangeSelet"
              :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
              :replaceFields="{ label: 'policeAgencyName', key: 'id', value: 'id' }"
            />
          </div>
        </div>

        <div class="input-wrap">
          <div class="label">提交时间:</div>
          <div class="input-item">
            <a-month-picker
              v-model="queryParam.value"
              :disabled-date="disabledDate"
              :allowClear="false"
              @change="timeChange"
              value-format="YYYY-MM"
            />
          </div>
        </div>

        <div class="btn-wrap">
          <a-button class="searchBtn" @click="searchQuery()" type="primary"> 搜索 </a-button>
          <a-button class="resetBtn" @click="searchReset()" type="primary"> 重置 </a-button>
        </div>

        <div class="btn-group">
          <a-button class="exportBtn" @click="handleExportXls(`${cusString}`, queryParam)" type="primary">
            导出Excel
          </a-button>
        </div>
      </div>

      <div class="table-wrap f1">
        <a-table
          bordered
          id="a-table"
          class="tableList"
          :scroll="{ x: 999 }"
          rowKey="policeUserNo"
          :columns="cusColumns"
          :dataSource="dataSource"
          :pagination="false"
        >
        </a-table>
      </div>
    </div>
  </div>
</template>

<script>
import topTitle from '@/components/topTitle'
import startEndTime from '@/components/startEndTime'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction } from '@/api/manage'
import { getOneFlagTreeList } from '@/api/api'
export default {
  name: 'policeInspector',
  components: { topTitle, startEndTime },
  mixins: [JeecgListMixin],
  data() {
    return {
      cusString: '',
      url: {
        list: '/company/message_tube/police_agency_task',
        exportXlsUrl: '/company/message_tube/police_agency_task/export',
      },
      curMonth: '',
      cusColumns: [],
      treeData: [],
      disableMixinCreated: true,
      columns: [
        {
          title: '序号',
          align: 'center',
          width: 100,
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
      ],
    }
  },
  mounted() {
    if (this.$route.query && this.$route.query.constructor === Object) {
      let params = this.$route.query
      this.queryParam = { ...params }
    }

    this.queryParam.value = this.$moment().format('YYYY-MM')
    this.timeChange()
    this.loadData()
    this.loadTree()
  },
  methods: {
    disabledDate(current) {
      return current && current > this.$moment().endOf('day')
    },

    timeChange(v, index) {
      this.$forceUpdate()
      this.curMonth = this.queryParam.value.split('-')[1]
      this.queryParam.startTime = this.$moment(this.queryParam.value).startOf('month').valueOf()
      this.queryParam.endTime = this.$moment(this.queryParam.value).endOf('month').valueOf()
    },

    searchReset() {
      this.queryParam = {}
      let params = this.$route.query
      this.queryParam = { ...params }
      this.queryParam.value = this.$moment().format('YYYY-MM')
      this.timeChange()
      this.loadData()
    },

    loadTree() {
      getOneFlagTreeList().then((res) => {
        if (res.code == 200) {
          this.treeData = res.result
        }
      })
    },

    handelChangeSelet() {
      this.$forceUpdate()
    },

    async loadData() {
      let { result } = await getAction(this.url.list, this.queryParam)
      this.cusColumns = this.columns.concat(result.columnList)
      this.allChildrenAddValue(this.cusColumns) //递归塞值
      this.dataSource = result.lineList

      let res = this.getNodeById(this.treeData, this.queryParam.policeAgencyId)
      this.cusString =
        (res == undefined ? `陕西省公安厅` : res.policeAgencyName) +
        `${this.queryParam.title}` +
        `${this.curMonth}月份` +
        '检查情况统计表'
    },

    allChildrenAddValue(node) {
      // 递归生成动态的目标columns
      for (const item of node) {
        if (item.title && item.title !== '序号') {
          item.width = 100
          item.align = 'center'
          item.customCell = this.customCell
        }

        if (item.title && item.title !== '序号' && !item.children && !item.ellipsis) {
          item.scopedSlots = { customRender: 'cusRander' }
        }
        if (item.children) {
          this.allChildrenAddValue(item.children)
        }
      }
      return node
    },

    customCell(record, rowIndex, column) {
      // column add from 3.0
      let _this = this
      return {
        on: {
          click: (event) => {
            let policeAgencyId = record.policeAgencyId
            let dataIndex = event.target.__vue__.column.dataIndex

            if (rowIndex !== 0 && dataIndex == 'policeAgencyName') {
              this.queryParam.policeAgencyId = policeAgencyId
              this.loadData()
            }
          },
        },
      }
    },

    getNodeById(list, id) {
      if (id == '') return { policeAgencyName: '暂无' }
      for (const item of list) {
        if (item.id === id) return item
        if (item.children) {
          const res = this.getNodeById(item.children, id)
          if (res) return res
        }
      }
    },

    goback() {
      this.$router.go(-1)
    },

    exportExcel() {},
  },
  destroy() {},
}
</script>

<style scoped lang="less">
/deep/ .ant-table-body {
  border: 1px solid #0a5297 !important;
  .ant-table-thead > tr > th,
  .ant-table-tbody > tr > td {
    border: 1px solid #0a5297 !important;
  }
}

/deep/ .ant-table-row-cell-break-word:hover {
  background: #1388ff !important;
  cursor: pointer;
}

.goback {
  width: 60px;
  height: 30px;
  font-size: 16px;
  color: #0e2b48;
  background: #1388ff;
  border-radius: 4px;
  line-height: 30px;
  text-align: center;
  position: absolute;
  right: 0px;
  cursor: pointer;
}

.app-container {
  background: var(--theme-content-bg);
  padding-top: 0;
  padding-bottom: 0;
  .content-wrap {
    background: var(--theme-container-bg);
    overflow: hidden;
    .table-wrap {
      padding: 0 20px;
      overflow: auto;
    }
  }
}
</style>
